package com.framework.main.sys.indexFream.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.framework.base.service.BaseServiceImp;
import com.framework.base.util.DateUtil;
import com.framework.base.util.Struts2Utils;
import com.framework.main.sys.indexFream.dao.IndexFreamDao;
import com.framework.main.sys.indexFream.domain.IndexFreamVO;
import com.framework.main.sys.user.domain.UserVo;
import com.framework.main.sys.user.service.UserService;

/**
 * @Title: IndexFreamServiceImp.java
 * @Package com.framework.main.sys.indexFream.service
 * @Description:首页框架业务逻辑处理层实现.用来获取待办公文，通知公告，文件交流以及统计三项的总数
 * @jdk version 1.6
 * @author www.51wuxia.com
 * @version V1.0
 */

public class IndexFreamServiceImp extends BaseServiceImp implements
		IndexFreamService {

	
	private UserService userService;



	/*
	 * <p>Title: getReceiverNoReader</p> <p>Description: </p>
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see
	 * com.framework.main.sys.indexFream.service.IndexFreamService#getReceiverNoReader
	 * ()
	 */

	public List<?> getReceiverNoReader() throws Exception {
		UserVo user = Struts2Utils.getCurrentUser();
		return ((IndexFreamDao) this.getBaseDao()).getReceiverNoReader(user
				.getIdf());
	}


	public int getAnnouncementCount() throws Exception {
		StringBuffer sql=new StringBuffer(" where recipientIdf like '%");
		sql.append(Struts2Utils.getCurrentUser().getIdf()).append("%' ");
		sql.append("or recipientIdf='quanti'");
		return ((IndexFreamDao) this.getBaseDao()).getAnnouncementCount(sql.toString());
	}

	public int getReceiverNoReaderCount() throws Exception {
		UserVo user = Struts2Utils.getCurrentUser();
		return ((IndexFreamDao) this.getBaseDao())
				.getReceiverNoReaderCount(user.getIdf());
	}

	public List<?> getPerScherdule() throws Exception {
		UserVo user = Struts2Utils.getCurrentUser();
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dateTime = new Date();
		String startTime = sdf.format(dateTime);
		cal.add(Calendar.DATE, 1);
		String endTime = sdf.format(cal.getTime());
		String sql = " recorderIdf='" + user.getIdf() + "' and recordTimef>='"
				+ startTime + "' and recordTimef<='" + endTime
				+ "' order by recordTimef";
		return ((IndexFreamDao) this.getBaseDao()).getPerScherdule(sql);
	}

	
	public List<?> getTopPreSCherdule() throws Exception {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dateTime = new Date();
		String startTime = sdf.format(dateTime);
		cal.add(Calendar.DATE, 1);
		String endTime = sdf.format(cal.getTime());
		String sql = "  recordTimef>='" + startTime + "' and recordTimef<='"
				+ endTime + "' order by recordTimef desc";
		return ((IndexFreamDao) this.getBaseDao()).getTopPreSCherdule(sql);
	}
	
	@SuppressWarnings("unchecked")
	public Map getOrganSchedule() throws Exception {
		List userList = userService.getUserByTrueNameOrId();
		Map organMap = new LinkedHashMap();
		for (int i = 0; i < userList.size(); i++) {
			UserVo user = (UserVo) userList.get(i);
			String sql = " recorderIDf='" + user.getIdf()
					+ "' and recordTimef='" + DateUtil.formatDate(new Date())
					+ "'";
			IndexFreamVO indexVo = (IndexFreamVO) ((IndexFreamDao) this
					.getBaseDao()).getPerScherduleD(sql);
			organMap.put(user.getTrueNamef(), indexVo);
			
		}
		return organMap;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


}
